package kt.com.bilin.coin.ad

import android.view.ViewGroup.MarginLayoutParams
import android.widget.FrameLayout
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.Observer
import androidx.lifecycle.OnLifecycleEvent
import com.bytedance.sdk.openadsdk.core.widget.TTCountdownView
import com.lb.mmv.ADCallBack
import com.lb.mmv.ADv
import kotlinx.coroutines.Job
import kt.com.bilin.coin.common.dp
import kt.com.bilin.coin.common.toastMessageLong
import kt.com.bilin.coin.ui.dialog.LoadingDialog
import kt.com.bilin.coin.utils.Logger
import kt.com.bilin.coin.utils.ViewUtils


object AdManager : LifecycleObserver {
    private const val TIME_OUT = 5500L// 最大等待时间
    private var mLoadingDialog: LoadingDialog? = null
    private var mOnVideoComplete: OnVideoComplete? = null
    private var mActivity: ComponentActivity? = null
    private var isStartSp = false
    private var spEnd = false
    private var launch: Job? = null
    private var mObserver: Observer<String>? = null
    private var isFirstVideo = true
    private var isFirstShowInner = true
    private var isShowInner = false
    fun init(activity: ComponentActivity?) {
//        if (!changeActivity(activity)) return
        //        AdManager.INSTANCE.init(this);
        ADv.initActivtiy(activity)
        //ADHandler.getIns().initActivity(mActivity, "blsq", null)
    }

    fun loadSplashAd(activity: ComponentActivity?, flSplashContainer: FrameLayout, onAdComplete: OnVideoComplete) {
        if (!changeActivity(activity)) return
        mOnVideoComplete = onAdComplete
//        flSplashContainer.viewTreeObserver.addOnGlobalLayoutListener {
//            val ttCountdownView: TTCountdownView? = ViewUtils.findViewByClass(flSplashContainer, TTCountdownView::class.java)
//            if (ttCountdownView != null && ttCountdownView.layoutParams is MarginLayoutParams) {
//                ttCountdownView.setCountDownTime(2)
//                val layoutParams = ttCountdownView.layoutParams as MarginLayoutParams
//                layoutParams.topMargin = 20.dp
//                ttCountdownView.layoutParams = layoutParams
//            }
//        }
        ADv.showSp(mActivity, flSplashContainer, object : ADCallBack {
            override fun click() { //用户点击
                //Log.e(TAG, "开屏用户点击")
                onDestroy()
            }

            override fun closeFail(code: Int, msg: String) { //展示失败，自动关闭或者用户跳过
//                Log.e(TAG, "开屏关闭")
                mOnVideoComplete?.invoke()
                onDestroy()
            }
        })
    }

    fun showInner(activity: ComponentActivity) {
        if (isFirstShowInner) {
            isFirstShowInner = false
            return
        }
        if (isShowInner) {
            return
        }
        isShowInner = true
        mActivity = activity
        ADv.showInner(mActivity)
        onDestroy()
    }

    fun loadRewardVideoAd(activity: ComponentActivity?, onAdComplete: OnVideoComplete) {
        if (!changeActivity(activity)) return
        mOnVideoComplete = onAdComplete
        mLoadingDialog?.show()
//        if (isFirstVideo) {
//            isFirstVideo = false
//            mActivity?.lifecycleScope?.launch {
//                delay(1000)
//                showVideo()
//            }
//        } else {
        showVideo()
//        }
    }

    private fun showVideo() {
        ADv.showVideo(mActivity, object : ADCallBack {
            override fun click() {
                mOnVideoComplete?.invoke()
                onDestroy()
                Logger.e("视频点击或者视频看完")
            }

            override fun closeFail(code: Int, msg: String) {
                msg.toastMessageLong()
                Logger.e("视频关闭")
                onDestroy()
            }
        })
        /*ADHandler.getIns().showVideo(mActivity, object : videoADCallBack {
            override fun showFullSucess() {
                mOnVideoComplete?.invoke()
                onDestroy()
                Logger.e("视频播放完毕")
            }

            override fun showFullFail(code: Int, message: String) {
                onDestroy()
                Logger.e("视频播放未成功")
                message.toastMessageLong()
            }
        })*/
    }

    private fun changeActivity(activity: ComponentActivity?): Boolean {
        if (activity == null) return false
        mActivity?.let { onDestroy() }
        mLoadingDialog?.dismiss()
        mLoadingDialog = null
        mActivity = activity
        mLoadingDialog = LoadingDialog(activity).apply { setCancelable(false) }
        return true
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy() {
        //ADHandler.getIns().destroy()
        mActivity?.lifecycle?.removeObserver(this)
        mActivity = null
        mOnVideoComplete = null
        mLoadingDialog?.dismiss()
        mLoadingDialog = null
        launch?.run { if (!isCancelled) cancel() }
    }
}